package com.speedchina.common.rsa.service.impl;

import com.speedchina.common.rsa.service.RSAService;
import com.speedchina.common.rsa.util.RSAUtils;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Winter
 */
@Service
public class RSAServiceImpl implements RSAService {
    private static Map<String,String> map = new ConcurrentHashMap();

    @Override
    public String getPrivateKey(String publicKey) {
        return map.get(publicKey);
    }

    @Override
    public boolean setPublicKeyPrivateKey(String publicKey, String privateKey) {
        try {
            map.put(publicKey,privateKey);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean removePublicKeyPrivateKey(String publicKey) {
        try {
            map.remove(publicKey);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Map<String, String> generatePublicKeyPrivateKey() throws NoSuchAlgorithmException {
        Map<String, String> map = RSAUtils.generatePublicKeyPrivateKey();
        setPublicKeyPrivateKey(map.get("publicKey"),map.get("privateKey"));
        return map;
    }
}
